package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.NoticeDTO;
import com.sky.dto.NoticePageQueryDTO;
import com.sky.entity.Notice;
import com.sky.exception.BaseException;
import com.sky.mapper.NoticeMapper;
import com.sky.result.PageResult;
import com.sky.service.NoticeService;
import com.sky.vo.NoticeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {

    private final NoticeMapper noticeMapper;

    public NoticeServiceImpl(NoticeMapper noticeMapper) {
        this.noticeMapper = noticeMapper;
    }

    @Override
    public void save(NoticeDTO noticeDTO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDTO, notice);

        notice.setCreateUser(BaseContext.getCurrentId());
        notice.setUpdateUser(BaseContext.getCurrentId());
        notice.setCreateTime(LocalDateTime.now());
        notice.setUpdateTime(LocalDateTime.now());

        noticeMapper.insert(notice);
    }

    @Override
    public PageResult pageQuery(NoticePageQueryDTO noticePageQueryDTO) {
        PageHelper.startPage(noticePageQueryDTO.getPage(), noticePageQueryDTO.getPageSize());
        Page<Notice> page = noticeMapper.pageQuery(noticePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public NoticeVO getById(Long id) {
        Notice notice = noticeMapper.getById(id);
        if (notice == null) {
            throw new BaseException(MessageConstant.NOTICE_NOT_FOUND);
        }

        NoticeVO noticeVO = new NoticeVO();
        BeanUtils.copyProperties(notice, noticeVO);
        return noticeVO;
    }

    @Override
    public void update(NoticeDTO noticeDTO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeDTO, notice);
        notice.setUpdateUser(BaseContext.getCurrentId());
        notice.setUpdateTime(LocalDateTime.now());

        noticeMapper.update(notice);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Notice notice = Notice.builder()
                .id(id)
                .status(status)
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();
        noticeMapper.update(notice);
    }

    @Override
    public NoticeVO getLatestNotice() {
        Notice notice = noticeMapper.getLatestNotice();
        if (notice == null) {
            return null;
        }

        NoticeVO noticeVO = new NoticeVO();
        BeanUtils.copyProperties(notice, noticeVO);
        return noticeVO;
    }

    @Override
    public PageResult pageQueryForUser(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        NoticePageQueryDTO dto = new NoticePageQueryDTO();
        dto.setStatus(1); // 强制设置为只查询启用状态

        Page<Notice> pageResult = noticeMapper.pageQuery(dto);
        return new PageResult(pageResult.getTotal(), pageResult.getResult());
    }

    @Override
    public NoticeVO getByIdForUser(Long id) {
        Notice notice = noticeMapper.getById(id);
        if (notice == null || notice.getStatus() == 0) {
            throw new BaseException(MessageConstant.NOTICE_NOT_FOUND);
        }

        NoticeVO noticeVO = new NoticeVO();
        BeanUtils.copyProperties(notice, noticeVO);
        return noticeVO;
    }

    @Override
    public void deleteNotice(Long id) {
        noticeMapper.deleteById(id);
    }


}